using System;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using DE.Sdc.SharePoint.SPContentStructure.Common;

namespace DE.Sdc.SharePoint.SPContentStructure.Import
{
    [Serializable]
    [XmlType(AnonymousType = true, Namespace = DeploymentConstants.Namespace)]
    [XmlRoot(Namespace = DeploymentConstants.Namespace, IsNullable = false)]
    public class Navigation 
    {
        private bool _inheritNavigation;

        public Navigation()
        {
            QuickLaunch = new HierarchicalList<NavigationItem, NavigationItem>(null);
            TopNavigationBar = new HierarchicalList<NavigationItem, NavigationItem>(null);
        }

        /// <remarks/>
        [XmlAttribute]
        public bool IncludePages { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public bool IncludeSubSites { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public bool IncludeHeadings { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public bool InheritNavigation
        {
            get { return InheritNavigationSpecified ? _inheritNavigation : true; }
            set
            {
                _inheritNavigation = value;
                InheritNavigationSpecified = true;
            }
        }

        /// <remarks/>
        [XmlIgnore]
        public bool InheritNavigationSpecified { get; set; }
        
        /// <remarks/>
        [XmlAttribute]
        public bool IncludeAuthoredLinks { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public NavigationSortMethodType SortMethod { get; set; }

        /// <remarks/>
        [XmlArrayItem("NavigationItem", typeof(NavigationItem))]
        //[XmlElement("NavigationItem", typeof(NavigationItem))]
        public HierarchicalList<NavigationItem, NavigationItem> QuickLaunch { get; set; }

        /// <remarks/>
        [XmlArrayItem("NavigationItem", typeof(NavigationItem))]
        //[XmlElement("NavigationItem", typeof(NavigationItem))]
        public HierarchicalList<NavigationItem, NavigationItem> TopNavigationBar { get; set; }

        /// <summary>
        /// Changes/Updates the Navigation
        /// </summary>
        /// <param name="navigationDefinition">Xml object for a navigation object</param>
        /// <param name="navigation">A sharepoint navigation object</param>
        protected internal static void ModifyNavigation(Navigation navigationDefinition, SPNavigation navigation)
        {
            try
            {
                SPWeb web = navigation.Web;

                BaseItem<NavigationItem>.ModifyProperty("__IncludeSubSitesInNavigation", navigationDefinition.IncludeSubSites.ToString(), false,
                               web.AllProperties);
                BaseItem<NavigationItem>.ModifyProperty("__IncludePagesInNavigation", navigationDefinition.IncludePages.ToString(), false,
                               web.AllProperties);
                BaseItem<NavigationItem>.ModifyProperty("__IncludeAuthoredLinksInNavigation", navigationDefinition.IncludeAuthoredLinks.ToString(), false,
                               web.AllProperties);
                BaseItem<NavigationItem>.ModifyProperty("__IncludeHeadingsInNavigation", navigationDefinition.IncludeHeadings.ToString(), false,
                               web.AllProperties);
                BaseItem<NavigationItem>.ModifyProperty("__InheritCurrentNavigation", navigationDefinition.InheritNavigation.ToString(), false,
                               web.AllProperties);

                if (!web.AllProperties.ContainsKey("__NavigationOrderingMethod"))
                {
                    BaseItem<NavigationItem>.ModifyProperty("__NavigationOrderingMethod", (int) navigationDefinition.SortMethod, false,
                                   web.AllProperties);
                }

                //if (navigationDefinition.SortMethod == NavigationSortMethodType.Manually)
                //{
                //    for (int index = 0; index < web.Webs.Count; index++)
                //    {
                //        SPWeb subWeb = web.Webs[index];

                //        var webNavigationNode = GetNavigationItem(subWeb.ServerRelativeUrl, parentNavigationItem);

                //        if (webNavigationNode == null)
                //        {
                //            CreateNavigationItem(new NavigationItem { Title = subWeb.Title, Url = subWeb.ServerRelativeUrl },
                //                                 parentNavigationItem, subWeb);
                //        }
                //    }
                //}

                if (navigationDefinition.QuickLaunch != null)
                    ImportNavigation(navigationDefinition.QuickLaunch, navigation.QuickLaunch.Parent);

                if (navigationDefinition.TopNavigationBar != null)
                    ImportNavigation(navigationDefinition.TopNavigationBar, navigation.TopNavigationBar.Parent);

                web.Update();
            }
            catch (Exception)
            {
                {
                }
                throw;
            }
        }

        private static void ImportNavigation(HierarchicalList<NavigationItem, NavigationItem> navigationItemDefinitions, SPNavigationNode parentNavigationItem)
        {
            for (int index = 0; index < navigationItemDefinitions.Count; index++)
            {
                NavigationItem navigationItemDefinition = navigationItemDefinitions[index];
                navigationItemDefinition.Title = DeploymentHelper.Resolve(navigationItemDefinition.Title, parentNavigationItem.Navigation.Web.Locale);
                navigationItemDefinition.Url = DeploymentHelper.Resolve(navigationItemDefinition.Url, parentNavigationItem.Navigation.Web.Locale);
                var navigationItem = NavigationItem.GetNavigationItem(navigationItemDefinition, parentNavigationItem);
                NavigationItem.ImportNavigationItem(navigationItemDefinition, navigationItem, parentNavigationItem);
            }
        }
    }
}